package com.nba.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.nba.bean.Schedule;
import com.nba.bean.inherit.GameInherit;
import com.nba.dao.GameDao;
import com.nba.util.DBUtil;

public class GameDaoImpl implements GameDao {
	
	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	
	@Override
	public List<Schedule> selectGameAll() {
		String sql = "SELECT * FROM schedule ";
		List<Schedule> list = jt.query(sql, new BeanPropertyRowMapper<Schedule>(Schedule.class));
		return list;
	}

	@Override
	public Schedule selectGameById(String gameId) {
		String sql = "SELECT * FROM schedule WHERE schedule_id = ?";
		List<Schedule> list = jt.query(sql, new BeanPropertyRowMapper<Schedule>(Schedule.class),gameId);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public List<GameInherit> SelectGameAll(String keyword, int pageNum, int pageSize) {
		String sql = "SELECT * FROM sstt WHERE season_name LIKE ? OR ateam_name LIKE ? OR bteam_name LIKE ? OR field LIKE ? OR schedule_time LIKE ? LIMIT ?,? ";
		List<GameInherit> list = jt.query(sql, new BeanPropertyRowMapper<GameInherit>(GameInherit.class),"%"+keyword+"%","%"+keyword+"%","%"+keyword+"%","%"+keyword+"%","%"+keyword+"%",(pageNum-1)*pageSize,pageSize);
		return list;
	}

	@Override
	public int selectGameTotal(String keyword) {
		String sql = "SELECT count(1) FROM sstt WHERE season_name LIKE ? OR ateam_name LIKE ? OR bteam_name LIKE ? OR field LIKE ? OR schedule_time LIKE ?";
		return jt.queryForObject(sql, Integer.class,"%"+keyword+"%","%"+keyword+"%","%"+keyword+"%","%"+keyword+"%","%"+keyword+"%");
	}

	@Override
	public int delectGameById(int gameId) {
		String sql = "DELETE FROM schedule WHERE schedule_id = ?";
		return jt.update(sql, gameId);
	}

	@Override
	public int updateGame(Schedule schedule) {
		String sql = "UPDATE schedule SET season_id = ? , ateam_id = ? , bteam_id =?, afirst_num = ?,bfirst_num=?,asecond_num = ?,bsecond_num =?,athird_num = ? ,bthird_num=? , afourth_num =?,bfourth_num = ?,afastbreak_score =?,bfastbreak_score=?,arestricted_score = ?,brestricted_score=?,afault_score =?,bfault_score=?,field=?,schedule_time=? WHERE schedule_id = ?";
		return jt.update(sql, schedule.getSeasonId(),schedule.getAteamId(),schedule.getBteamId(),schedule.getAfastbreakScore(),schedule.getBfastbreakScore(),schedule.getAsecondNum(),schedule.getBsecondNum(),schedule.getAthirdNum(),schedule.getBthirdNum(),schedule.getAfourthNum(),schedule.getBfourthNum(),schedule.getAfastbreakScore(),schedule.getBfastbreakScore(),schedule.getArestrictedScore(),schedule.getBrestrictedScore(),schedule.getAfaultScore(),schedule.getBfaultScore(),schedule.getField(),schedule.getScheduleTime(),schedule.getScheduleId());
	}

	@Override
	public int insertGame(Schedule schedule) {
		String sql = "INSERT INTO schedule (season_id,ateam_id,bteam_id,afirst_num,bfirst_num,asecond_num,bsecond_num,athird_num,bthird_num,afourth_num,bfourth_num,afastbreak_score,bfastbreak_score,arestricted_score,brestricted_score,afault_score,bfault_score,field,schedule_time) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		return jt.update(sql, schedule.getSeasonId(),schedule.getAteamId(),schedule.getBteamId(),schedule.getAfirstNum(),schedule.getBfirstNum(),schedule.getAsecondNum(),schedule.getBsecondNum(),schedule.getAthirdNum(),schedule.getBthirdNum(),schedule.getAfourthNum(),schedule.getBfourthNum(),schedule.getAfastbreakScore(),schedule.getBfastbreakScore(),schedule.getArestrictedScore(),schedule.getBrestrictedScore(),schedule.getAfaultScore(),schedule.getBfaultScore(),schedule.getField(),schedule.getScheduleTime());
	}

}
